using System.Linq;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_SelfAttackOther : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.SelfAttackOther;

        private bool triggerBySelf;
        private EDamageType damageType;
        private EDamageFrom damagefrom;
        private int desType;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_SelfAttackOther_ConfigData rConfigData)
            {
                this.damageType = (EDamageType)rConfigData.damageType;
                this.desType = rConfigData.desType;
                this.damagefrom = (EDamageFrom)rConfigData.damageFrom;
                this.triggerBySelf = rConfigData.triggerBySelf;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<DamageResponse, Skill>(EBattleTriggerType.Battle_SkillDamage, this.OnEvent);
        }

        private void OnEvent(DamageResponse rResp, Skill rSkill)
        {
            if (rResp.attacker == this.skill.executer.GUID && (this.triggerBySelf || this.skill != rSkill))
            {
                if (this.damageType != EDamageType.None && (rResp.damageType & this.damageType) == 0 ||
                    (this.damagefrom != EDamageFrom.None && (rResp.damageFrom & this.damagefrom) == 0))
                {
                    return;
                }

                var rDefender = this.skill.executer.battle.playCtrl.unitCtrl.GetUnit(rResp.defender);
                if (this.desType > 0)
                {
                    var rBuffComp = rDefender.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
                    bool bWithBuff = false;
                    for (int j = 0; j < rBuffComp.buffList.Count; j++)
                    {
                        var rBuff = rBuffComp.buffList[j];
                        if (rBuff.config.Tag.Contains(this.desType))
                        {
                            bWithBuff = true;
                            break;
                        }
                    }

                    if (!bWithBuff)
                    {
                        return;
                    }
                }

                var rVirInput = ClassPool.Get<SelectParam>();
                rVirInput.unit = rDefender;
                rVirInput.pos = rVirInput.unit.pos;
                this.OnTrigger(rVirInput, rResp);
            }
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return $"自己攻击{(this.damageType >= 0 ? $"伤害类型:[{this.damageType}]," : "")}{(this.damagefrom >= 0 ? $"伤害来源:[{this.damagefrom}]," : "")}{(this.desType >= 0 ? $"目标包含Buff类型:[{this.desType}]," : "")}的攻击";
        }
#endif
    }
}